package com.kdtech.analyse.NewsPaper;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://epaper.oeeee.com 南方都市报
 * @author Persh
 */
public class OeeeeNewsPaperAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.oeeee.com/epaper/.*/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm.*",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta newspaper = new NewsMeta();

		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		newspaper.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Document doc = Jsoup.parse(html);

		title =doc.select("div.article h1").text();
		String tempdate=url;
		date=DateUtils.matchDate(tempdate);

		content =HtmlCleaner.getContentHtml(url,doc.select("div.text"));
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}

	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.oeeee.com/epaper/.*/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_[0-9]*.htm",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
/*
	public static void main(String[] args) throw {
		OeeeeNewsPaperAnalyse a = new OeeeeNewsPaperAnalyse();
		String url = "http://epaper.oeeee.com/epaper/A/html/2015-11/12/content_10332.htm";
		System.out.println(a.isDetailPage(url));
		System.out.println(a.parserHtml(CrawlHTML.responseToURL(url)));;
		System.out.println(CrawlHTML.getLinkList(CrawlHTML.responseToURL(url)));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		HashMap<String, String> linkList = CrawlHTML.getLinkList(meta);
		Set<Entry<String, String>> entrySet = linkList.entrySet();
		for (Entry<String, String> entry : entrySet) {
			String url2 = entry.getKey();
			if(a.isTaskPage(url2)){
				meta = CrawlHTML.responseToURL(url2);
				HashMap<String, String> linkList2 = CrawlHTML.getLinkList(meta);
				for (Entry<String, String> entry2 : linkList2.entrySet()) {
					String url3 = entry2.getKey();
					if(a.isDetailPage(url3)){
						meta = CrawlHTML.responseToURL(url3);
						NewsMeta parserHtml = a.parserHtml(meta);
						System.out.println(parserHtml);
					}
				}
			}
		}
		NewsMeta parserHtml = a.parserHtml(meta);
		System.out.println(parserHtml);
	}

*/



}
